package com.niuwa.p2p.service.impl.credit;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.utils.MobileUtil;
import com.niuwa.p2p.entity.credit.CollegesInfo;
import com.niuwa.p2p.entity.credit.ProfessionInfo;
import com.niuwa.p2p.service.credit.CollegesListService;
import com.niuwa.p2p.service.credit.CollegesService;
import com.niuwa.p2p.service.credit.ProfessionService;

@Service
public class CollegesListServiceImpl implements CollegesListService {

	@Autowired
	private CollegesService collegesService;

	@Autowired
	private ProfessionService professionService;

	@Override
	public void getCollegesList() {
		int count=0;
		// 获取list数据[组装url动态地址]
		ArrayList<Integer> baseUrls = new ArrayList<Integer>(Arrays.asList(11,
				12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42,
				43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65));
		for (Integer baseUrl : baseUrls) {
			for (int i = 0; i < 10; i++) {
				// 20条来进行分页
				int secondUrl = i * 20;
				String url = "http://gaokao.chsi.com.cn/sch/search--ssdm-"
						+ baseUrl + ",ss-on,searchType-1,option-qg,start-"
						+ secondUrl + ".dhtml";
				try {
					Document doc = Jsoup.connect(url).timeout(5000).get();

					Elements elems = doc.getElementsByClass("search");
					for (Element elem : elems) {
						Elements elemsBodys = elem.getElementsByTag("table");
						if (elemsBodys.size() >= 2) {
							int next = 0;
							for (Element elemsBody : elemsBodys) {
								if (next == 1) {
									Elements elemsTrs = elemsBody
											.getElementsByTag("tr");
									for (Element elemsTr : elemsTrs) {
										String yxmc = null;
										String szd = null;
										String yxls = null;
										String xlcc = null;
										String bxlx = null;
										String yxlx = null;
										String zszy = null;
										Boolean eyy = false;
										Boolean jbw = false;
										Boolean yjs = false;
										Elements elemsTds = elemsTr
												.getElementsByTag("td");
										for (Element elemsTd : elemsTds) {
											if (elemsTd.getElementsByTag(
													"strong").size() > 0) {
												continue;
											} else {
												// 985 211 和在研
												Elements elemsSpans = elemsTd
														.getElementsByTag("span");
												if (elemsSpans.size() > 0) {
													for (Element elemsSpan : elemsSpans) {
														if ("985"
																.equals(elemsSpan
																		.text())) {
															jbw = true;
														}
														if ("211"
																.equals(elemsSpan
																		.text())) {
															eyy = true;
														}
														if ("研".equals(elemsSpan
																.text())) {
															yjs = true;
														}
													}
												} else {
													yxmc = elemsTds
															.get(0)
															.getElementsByTag(
																	"a").text();
													szd = elemsTds.get(1)
															.text();
													yxls = elemsTds.get(2)
															.text();
													xlcc = elemsTds.get(3)
															.text();
													bxlx = elemsTds.get(4)
															.text();
													yxlx = elemsTds.get(5)
															.text();
													zszy = getSyzyUrl(elemsTds
															.get(6).html());
												}

											}
										}
										if (null != yxmc) {
											count++;
											System.out.println("院校名称:" + yxmc
													+ "是否985:" + jbw + "是否211:"
													+ eyy + "是否有研:" + yjs
													+ "数量地址:" + zszy);
											// 拿到专业地址进行专业地址解析
											Set<String> sets = jsopZy(zszy, yxmc);
											CollegesInfo collegesInfo=new CollegesInfo();
											collegesInfo.setId(Long.valueOf(count));
											collegesInfo.setCollegeName(yxmc);
											collegesInfo.setEyy(eyy);
											collegesInfo.setJbw(jbw);
											collegesInfo.setYan(yjs);
											collegesInfo.setCollegeUnder(yxls);
											collegesInfo.setLocation(szd);
											collegesInfo.setSchooling(xlcc);
											collegesInfo.setSchoolTypes(bxlx);
											collegesInfo.setCollegesTypes(yxlx);
											collegesInfo.setProfessionCount(sets.size());
											collegesService.insert(collegesInfo);
											//遍历set入库到专业库
											Iterator<String> it = sets.iterator();  
											while (it.hasNext()) {  
											  String str = it.next();  
											    ProfessionInfo professionInfo=new ProfessionInfo();
												professionInfo.setCollegeId(Long.valueOf(count));
												professionInfo.setCollegeName(yxmc);
												professionInfo.setProfessionName(str); 
												professionService.insert(professionInfo);
											}  
											
										}

									}
								}
								next++;
							}
						} else {
							continue;
						}
					}
				} catch (IOException e) {

					e.printStackTrace();
				}
			}

		}

	}

	/**
	 * 解析专业到数据库
	 * 
	 * @param url
	 */
	public static Set<String> jsopZy(String url, String collgeName) {
		Set<String> set = new HashSet<String>();
		try {
			Document doc = Jsoup.connect(url).timeout(5000).get();
			Elements elems = doc.getElementsByClass("search");
			for (Element elem : elems) {
				Elements elemsBodys = elem.getElementsByTag("table");
				for (Element elemsBody : elemsBodys) {
					Elements elemsTrs = elemsBody.getElementsByTag("tr");
					for (Element elemsTr : elemsTrs) {
						Elements elemsTds = elemsTr.getElementsByTag("td");
						for (Element elemsTd : elemsTds) {
							String elemsTdText = elemsTd.text();
							if (MobileUtil.isEmpty(elemsTdText)
									|| elemsTdText.hashCode() == 160) {
								continue;
							}
							set.add(elemsTdText);
						}
					}
				}
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return set;
	}

	private String getSyzyUrl(String strings) {
		String[] zszyStrs = strings.split("href=\"");
		String[] zszyUrls = zszyStrs[1].split("dhtml");
		StringBuffer buffer = new StringBuffer();
		buffer.append("http://gaokao.chsi.com.cn");
		buffer.append(zszyUrls[0]);
		buffer.append("dhtml");
		return buffer.toString();
	}

	public static void main(String[] args) {
		CollegesListServiceImpl impl = new CollegesListServiceImpl();
		impl.getCollegesList();
	}
}
